<?php
namespace app\admin\logic;

class PatrolTotalLogic extends BaseLogic {

    /**
     * 获取巡店统计查询条件
     * @param $branchid
     * @return array
     */
    public function get_search_pt_maps($branchid){
        $maps = [];

        if($branchid != -1){
            $maps[] = ['id','=',$branchid];
        }

        return $maps;
    }

    /**
     * 获取巡店分部查询条件
     * @param $branchid
     * @param $start_time
     * @param $end_time
     * @return array
     */
    public function get_search_cb_maps($branchid,$start_time,$end_time){
        $maps = [];
        $maps[] = ['branchid','=',$branchid];
        $maps[] = ['time_code','<>','other'];

        if(!empty($start_time) && !empty($end_time)){
            $start_date = date('Y-m-d',strtotime($start_time));
            $end_date = date('Y-m-d',strtotime($end_time));

            $maps[] = ['now_date','between',[$start_date,$end_date]];
        }
        $maps[] = ['holiday','=',0];
        return $maps;
    }

    /**
     * 计算分部社区应巡店次数
     * @param $branchid
     * @param $start_time
     * @param $end_time
     * @return mixed
     */
    public function count_cp_branch_must_num($branchid,$start_time,$end_time){
        $now_date = date('Y-m-d');

        $maps = $this->get_search_cb_maps($branchid,$start_time,$end_time);
        $maps[] = ['now_date','<',$now_date];

        $num = model('cp_branch')->get_count($maps,'id');

        $data['must_num'] = $num;
        return $this->get_response(1,'OK',$data);
    }

    /**
     * 计算分部社区已巡店次数
     * @param $branchid
     * @param $start_time
     * @param $end_time
     * @return mixed
     */
    public function count_cp_branch_already_num($branchid,$start_time,$end_time){
        $maps = $this->get_search_cb_maps($branchid,$start_time,$end_time);
        $maps[] = ['status','=',1];

        $already_num = model('cp_branch')->get_count($maps,'id');

        $data['already_num'] = $already_num;
        return $this->get_response(1,'OK',$data);
    }

    /**
     * 计算分部社区未巡店次数
     * @param $branchid
     * @param $start_time
     * @param $end_time
     * @return mixed
     */
    public function count_cp_branch_no_num($branchid,$start_time,$end_time){
        $now_date = date('Y-m-d');

        $maps = $this->get_search_cb_maps($branchid,$start_time,$end_time);
        $maps[] = ['now_date','<',$now_date];
        $maps[] = ['status','=',0];

        $no_num = model('cp_branch')->get_count($maps,'id');

        $data['no_num'] = $no_num;
        return $this->get_response(1,'OK',$data);
    }
}